﻿using System;

namespace KKCai.Utils
{
    public class TypeConvert
    {
        public static int StringToInt(string value)
        {
            return StringToInt(value, 0);
        }

        public static int StringToInt(string value, int defaultValue)
        {
            if (String.IsNullOrEmpty(value))
            {
                return defaultValue;
            }

            try
            {
                return Convert.ToInt32(value);
            }
            catch (FormatException)
            {
                return defaultValue;
            }
        }

        public static bool StringToBool(string value)
        {
            return StringToBool(value, false);
        }

        public static bool StringToBool(string value, bool defaultValue)
        {
            if (String.IsNullOrEmpty(value))
            {
                return defaultValue;
            }

            try
            {
                return Convert.ToBoolean(value);
            }
            catch (FormatException)
            {
                return defaultValue;
            }
        }

        public static DateTime StringToDateTime(string value)
        {
            return StringToDateTime(value, DateTime.Now);
        }

        public static DateTime StringToDateTime(string value, DateTime defaultValue)
        {
            if (String.IsNullOrEmpty(value))
            {
                return defaultValue;
            }

            try
            {
                return Convert.ToDateTime(value);
            }
            catch (FormatException)
            {
                return defaultValue;
            }
        }
    }
}
